package com.donica.wamstest.utils;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.donica.wamstest.common.Constant;
import com.donica.wamstest.entity.AirfleetVO;
import com.donica.wamstest.entity.AirplaneVO;
import com.donica.wamstest.entity.DeviceVO;
import com.donica.wamstest.entity.LogVO;
import com.donica.wamstest.entity.TaskStartVO;
import com.donica.wamstest.entity.TaskVO;
import com.donica.wamstest.entity.UserVO;

public class LogUtil {
	public static final String LEVEL_INFO = "INFO";
	// public static final String LEVEL_ERROR = "ERROR";

	public static final String ACTION_LOGIN = "login";
	public static final String ACTION_LOGOUT = "logout";

	public static final String ACTION_ADD_USER = "addUser";
	public static final String ACTION_UPDATE_USER = "updateUser";
	public static final String ACTION_DELETE_USER = "deleteUser";

	public static final String ACTION_ADD_AIRFLEET = "addAirfleet";
	public static final String ACTION_UPDATE_AIRFLEET = "updateAirfleet";
	public static final String ACTION_DELETE_AIRFLEET = "deleteAirfleet";

	public static final String ACTION_ADD_AIRPLANE = "addAirplane";
	public static final String ACTION_UPDATE_AIRPLANE = "updateAirplane";
	public static final String ACTION_DELETE_AIRPLANE = "deleteAirplane";

	public static final String ACTION_ADD_DEVICE = "addDevice";
	public static final String ACTION_UPDATE_DEVICE = "updateDevice";
	public static final String ACTION_DELETE_DEVICE = "deleteDevice";

	public static final String ACTION_DELETE_DEVICEINFO = "deleteDeviceInfo";

	public static final String ACTION_DOWNLOAD_PROGRAM = "downloadProgram";
	public static final String ACTION_ACTIVE_PROGRAM = "activeProgram";

	public static final String ACTION_DOWNLOAD_SOFTWARE = "downloadSoftware";
	public static final String ACTION_ACTIVE_SOFTWARE = "activeSoftware";

	public static final String ACTION_ADD_TASK = "startTask";

	@SuppressWarnings("unchecked")
	public static <T> List<LogVO> getLog(String action, int result, List<T> volist) {
		if (action.equals(ACTION_ADD_USER) || action.equals(ACTION_UPDATE_USER) || action.equals(ACTION_DELETE_USER)
				|| action.equals(ACTION_LOGIN) || action.equals(ACTION_LOGOUT)) {
			return getUserLog(action,(List<UserVO>) volist);
		} else if (action.equals(ACTION_ADD_AIRFLEET) || action.equals(ACTION_UPDATE_AIRFLEET)
				|| action.equals(ACTION_DELETE_AIRFLEET)) {
			return getAirfleetLog(action, (List<AirfleetVO>) volist);
		} else if (action.equals(ACTION_ADD_AIRPLANE) || action.equals(ACTION_UPDATE_AIRPLANE)
				|| action.equals(ACTION_DELETE_AIRPLANE)) {
			return getAirplaneLog(action, (List<AirplaneVO>) volist);
		} else if (action.equals(ACTION_ADD_DEVICE) || action.equals(ACTION_UPDATE_DEVICE)
				|| action.equals(ACTION_DELETE_DEVICE)) {
			return getDeviceLog(action, (List<DeviceVO>) volist);
		}  else {
			return new ArrayList<LogVO>();
		}
	}

	public static List<LogVO> getUserLog(String action,List<UserVO> volist) {
		List<LogVO> logList = new ArrayList<LogVO>();

		for (UserVO userVO : volist) {

			LogVO logVO = new LogVO();
			logVO.setAction(action);
			logVO.setLevel(LEVEL_INFO);
			logVO.setOperated(userVO.getId());
			logVO.setCreator(userVO.getCreator());
			String data = JSON.toJSONString(userVO);
			logVO.setData(data);
			logList.add(logVO);
		}

		return logList;
	}

	public static List<LogVO> getAirfleetLog(String action,List<AirfleetVO> volist) {
		List<LogVO> logList = new ArrayList<LogVO>();

		for (AirfleetVO fleetVO : volist) {

			LogVO logVO = new LogVO();
			logVO.setAction(action);
			logVO.setLevel(LEVEL_INFO);
			logVO.setOperated(fleetVO.getCode());
			logVO.setCreator(fleetVO.getCreator());
			String data = JSON.toJSONString(fleetVO);
			logVO.setData(data);
			logList.add(logVO);
		}

		return logList;
	}

	public static List<LogVO> getAirplaneLog(String action,List<AirplaneVO> volist) {
		List<LogVO> logList = new ArrayList<LogVO>();

		for (AirplaneVO planeVO : volist) {

			LogVO logVO = new LogVO();
			logVO.setAction(action);
			logVO.setLevel(LEVEL_INFO);
			logVO.setOperated(planeVO.getCode());
			logVO.setCreator(planeVO.getCreator());
			String data = JSON.toJSONString(planeVO);
			logVO.setData(data);
			logList.add(logVO);
		}

		return logList;
	}

	public static List<LogVO> getDeviceLog(String action,List<DeviceVO> volist) {
		List<LogVO> logList = new ArrayList<LogVO>();

		for (DeviceVO deviceVO : volist) {

			LogVO logVO = new LogVO();
			logVO.setAction(action);
			logVO.setLevel(LEVEL_INFO);
			logVO.setOperated(deviceVO.getCode());
			logVO.setCreator(deviceVO.getCreator());
			String data = JSON.toJSONString(deviceVO);
			logVO.setData(data);
			logList.add(logVO);
		}

		return logList;
	}

	public static List<LogVO> getTaskLog(String action,TaskStartVO vo) {
		List<LogVO> logList = new ArrayList<LogVO>();

		for (String codeTmp : vo.getObject_codes()) {
			LogVO logVO = new LogVO();

			if (action.equals(ACTION_ADD_TASK)) {
				if (vo.getTask_code().equals(Constant.TASK_CODE_PROGRAM_DOWNLOAD)) {
					logVO.setAction(ACTION_DOWNLOAD_PROGRAM);
				} else if (vo.getTask_code().equals(Constant.TASK_CODE_PROGRAM_UPDATE)) {
					logVO.setAction(ACTION_ACTIVE_PROGRAM);
				} else if (vo.getTask_code().equals(Constant.TASK_CODE_SOFTWARE_DOWNLOAD)) {
					logVO.setAction(ACTION_DOWNLOAD_SOFTWARE);
				} else if (vo.getTask_code().equals(Constant.TASK_CODE_SOFTWARE_UPDATE)) {
					logVO.setAction(ACTION_ACTIVE_SOFTWARE);
				} else {
					logVO.setAction(action);
				}
			}
			
			logVO.setLevel(LEVEL_INFO);
			logVO.setOperated(codeTmp + '/' + vo.getUri());
			logVO.setCreator(vo.getCreator());
			String data = JSON.toJSONString(vo);
			logVO.setData(data);
			logList.add(logVO);
		}
		
		return logList;
	}
}
